package csu.fun.spring_store.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import csu.fun.spring_store.domain.*;
import csu.fun.spring_store.persistence.CategoryMapper;
import csu.fun.spring_store.persistence.ItemMapper;
import csu.fun.spring_store.persistence.OrderMapper;
import csu.fun.spring_store.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@SessionAttributes(value = "User")
public class HelloController {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;

    @GetMapping("/")
    public String hello() {
        return "login";
    }

    @GetMapping("/shouye")
    public String shouye(Model model,
                         ModelMap modelMap ){
       Signon signon =(Signon) modelMap.get("User");
            return "shouye.html";
    }

    @GetMapping("/login")
    public String login(@Validated @ModelAttribute Signon signon,
                        BindingResult bindingResult
            , Model model) {
        String validationmsg = "";
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                validationmsg += error.getDefaultMessage();
            }
            model.addAttribute("errormsg", validationmsg);
            return "login.html";
        }
        model.addAttribute("username", signon.getUsername());
        model.addAttribute("User", signon);
        return "redirect:/AccountForm";
    }



    @GetMapping("/GoodsForm")
    public String GoodsForm(Model model,ModelMap modelMap) {

        Signon signon =(Signon) modelMap.get("User");
        List<Category> categories=categoryMapper.selectList(null);

//      for (int i=0;i<categories.size();i++)
//      {
//          categories.get(i).setDescn("<img src=\"/images/"+categories.get(i).getDescn()+"\" >");
//
//      }

        model.addAttribute("category",categories);
        return "GoodsManagement.html";
    }




    @RequestMapping("/main")
    public String hello2(){
            return "main";
        }


    @GetMapping("/OrderForm")
    public String OrderForm(Model model,ModelMap modelMap) {

        List<OrderVO> orders = orderMapper.selectOrders();
        for  ( int  i  =   0 ; i  <  orders.size()  -   1 ; i ++ )  {
            orders.get(i).setItemId(orders.get(i).getItemId()+"*"+orders.get(i).getQuantity());
            for  ( int  j  =  orders.size()  -   1 ; j  >  i; j -- )  {

                if  (orders.get(j).getOrderid()==(orders.get(i).getOrderid()))  {
                    orders.get(i).setItemId(orders.get(i).getItemId()+","+"\n"+orders.get(j).getItemId()+"*"+orders.get(j).getQuantity());
                    orders.remove(j);
                }
            }
        }
        model.addAttribute("orders", orders);
        List<Category> categories=categoryMapper.selectList(null);
        model.addAttribute("category",categories);


        for(int i=0;i<categories.size();i++)
        {
            QueryWrapper<Product> wrapper=new QueryWrapper();
            wrapper.eq("category",categories.get(i).getCategoryid());

            List<Product> productList=productMapper.selectList(wrapper);
            model.addAttribute(categories.get(i).getCategoryid(),productList);

                for(int k=0;k<productList.size();k++)
            {
                QueryWrapper<Item> wrapper2=new QueryWrapper();
                wrapper2.eq("productid",productList.get(k).getProductid());
                List<Item> itemList=itemMapper.selectList(wrapper2);

                model.addAttribute(productList.get(k).getProductid(),itemList);
            }
        }

        return "OrderManagement.html";
    }

    @GetMapping("/signout")
    public String signout(SessionStatus status) {
        status.setComplete();
        return "login.html";
    }


    @GetMapping("/registerForm")
    public String registerForm() {
        return "NewUserForm.html";
    }



    @GetMapping("/register")
    public String register(@RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("repeatpassword") String repassword,
                           Model model) {
        String regitermsg="";
        if(username=="")
        {
            regitermsg="用户名不能为空";
            model.addAttribute("regitermsg", regitermsg);
            return "NewUserForm.html";
        }
        if(password=="")
        {
            regitermsg="密码不能为空";
            model.addAttribute("regitermsg", regitermsg);
            return "NewUserForm.html";
        }
        if(repassword=="")
        {
            regitermsg="第二次密码不能为空";
            model.addAttribute("regitermsg", regitermsg);
            return "NewUserForm.html";
        }
        if(!(repassword.equals(password)))
        {
            regitermsg="两次密码不同";
            model.addAttribute("regitermsg", regitermsg);
            return "NewUserForm.html";
        }
        Signon signon =new Signon();
        signon.setUsername(username);
        signon.setPassword(password);
        model.addAttribute("User", signon);
        return "redirect:/AccountForm";

    }

    @RequestMapping("/test")
    @ResponseBody
    public Category test(){
        Category category=categoryMapper.getCategoryById("FISH");
        return category;
    }
    @RequestMapping("/test2")
    @ResponseBody
    public Product test2(){
        Product category=productMapper.getProductById("FI-FW-02");
        return category;
//        Item item=itemMapper.selectByproductid("");
//        return item;
    }
    @RequestMapping("/lirun")
    public String lirun(){

        return "lirun";
    }
}

